<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单例模式</title>
</head>
<body>
//1、独立的对象，建2个。一个xiaowang ,一个是xiaoli
//2\让Xiaoli跟xiaowang通过门铃进行通信
//3\先看一个xiaowang家有没有门，如果有门直接通过门铃通讯didi  如果没有先建门
//4、两个单例之间开始通信
<button id="a" onclick="clickA">a</button>
<button id="b">b</button>
<button id="c">c</button>
<button id="d">d</button>
<button id="e">e</button>
<button id="f">f</button>
<script>
    var xiaowang=(function(argument){
        var xiaowangjia=function(message){
            this.menling=message;
        }
        var men;
        var info={
            sendMessage:function(message){
                if(!men){
                    men= new xiaowangjia(message)
                }
                return men;
            }
        };
        return info;
    })();
    var xiaoli = {

        callXiaowang:function(msg){
            var _xw=xiaowang.sendMessage(msg);
            alert(_xw.menling);
            _xw=null;//等待垃圾回收
       }
    };
    xiaoli.callXiaowang('didi');
</script>
<script>
    //页面上6个按钮
    //abc=>top
    //def=>banner
    var top={
        init:function(){

        },
        a:4,
        render:function(){
          var me=this;
          me.btna=$('#a');
        },
        bind:function(){
            var me=this;
            me.btna.click(function(){
                //业务逻辑取出去
                me.test();
            })
        },
        test:function(){
            a:5;
        }
    }
    var banner={
        init:function(){

        },
        a:4,
        render:function(){
            var me=this;
            me.btna=$('#d');
        },
        bind:function(){
            var me=this;
            me.btna.click(function(){
                //业务逻辑取出去
                me.test();
            })
        },
        test:function(){
            a:6;
        }
    }
   var clickA=(function(){
        var a = 4;
    });
//    $('#b').click(function(){
//
//    });
//    $('#c').click(function(){
//
//    });
//    $('#d').click(function(){
//
//    });
//    $('#e').click(function(){
//
//    });
//    $('#f').click(function(){
//
//    });
</script>
<script>

    var Singleton = (function(){
        var instantiated;
        function init(){
            /*这里定义单例代码*/
            return{
                publicMethod:function(){
                    console.log("hello world");
                },
                publicProperty:"test"
            };
        }

        return{
            getInstance:function(){
                if(!instantiated){
                    instantiated = init();
                }
                return instantiated;
            }
        }
    })();

    Singleton.getInstance().publicMethod();
</script>

</body>
</html>